package whu.cn.whushare.ui.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.io.InputStream;
import java.net.URL;

import whu.cn.whushare.R;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.util.MyTagHandler;

/**
 * Created by xiaoping on 2015/4/30.
 */
@ViewMapping(id= R.layout.activity_show)
public class ActivityShow  extends BaseActivity {
    @ViewMapping(id=R.id.activity_content)
    private TextView mActivityContent;
    @ViewMapping(id=R.id.activity)
    private TextView mActivityName;
    private String activityContent;
    private String activityName;
    private String html2="<p>";
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        getIntentData();
        initView();

    }

    private void initView() {
        View root= ViewMappingUtil.mapView(this,this);
        getTopBar(root);
        mTopBar.title.setText("活动展示");
        final String html= activityContent+"";

        if(html.substring(0,3).equals("<p>")){
            String[] html1=html.split(">");
            for(int i=1; i <html1.length;i++)
            {
                html2=html2+html1[i]+">"+"<br>";
            }
        }
        else{

            html2=html;

        }

        final String html3=html2;

        new AsyncTask<String,String,Spanned>(){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Spanned s) {
                super.onPostExecute(s);

                mActivityContent.setText(s);
            }

            @Override
            protected Spanned doInBackground(String... params) {

               Spanned sp = Html.fromHtml(html3,
                        new Html.ImageGetter() {
                            @Override
                            public Drawable getDrawable(String source) {
                                source = "http://www.027team.com" + source;
                                InputStream is = null;
                                try {
                                    is = new URL(source).openStream();
                                    Drawable d = Drawable.createFromStream(is, "src");
                                    d.setBounds(0, 0, d.getIntrinsicWidth()*3, d.getIntrinsicHeight()*3);
                                    is.close();
                                    return d;
                                } catch (Exception e) {
                                    return null;
                                }
                            }
                        }, new MyTagHandler(ActivityShow.this));
                return sp;
            }
        }.execute();

        mActivityContent.setMovementMethod(LinkMovementMethod.getInstance());
        mActivityName.setText(activityName);

    }
    public static Intent getStartIntent(Context context,String activityContent,String activityName){
        Intent i=new Intent(context, ActivityShow.class);
        i.putExtra("activityContent",activityContent);
        i.putExtra("activityName",activityName);
        return i;


    }
    private void getIntentData(){
        if(getIntent()!=null){
            activityContent=getIntent().getStringExtra("activityContent");
            activityName=getIntent().getStringExtra("activityName");
        }
    }



}
